/*
 * Copyright (C) 2011 by Fabien Barbero
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.desktop;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import com.flickr.api.ApiKey;
import com.flickr.api.ApiKeyImpl;
import com.flickr.desktop.core.FlickrCoreModule;
import com.flickr.desktop.core.bootstrap.Bootstrap;
import com.flickr.desktop.core.config.FlickrConfiguration;
import com.flickr.desktop.core.config.FlickrConfigurationImpl;
import com.flickr.desktop.core.exit.ExitService;
import com.flickr.desktop.gui.FlickrGuiController;
import com.flickr.desktop.gui.FlickrGuiModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.io.File;
import java.util.logging.LogManager;
import org.codeartisans.java.sos.views.swing.helpers.SwingHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Fabien Barbero
 */
public class FlickrDesktop {
    private static final Logger LOGGER = LoggerFactory.getLogger(FlickrDesktop.class);
    
    
    public static void main(String[] args) {
        try {
            configureLogs();
            
            File configFile = new File("etc/flickr.conf");
            FlickrConfiguration config;
            if(configFile.exists()) {
                config = new FlickrConfigurationImpl(new File("etc/flickr.conf"));
                config.load();
            } else {
                config = new FlickrConfigurationImpl();
                config.setExternalBrowserExecutable(new File("/usr/bin/chromium"));
            }
            
            ApiKey apiKey = new ApiKeyImpl("41c05f2a296a5335", "430c301f129ea1eca81e582236f165fa");
            
            SwingHelper.initLookAndFeel(config.getLookAndFeel());

            Injector injector = Guice.createInjector(new FlickrCoreModule(config, apiKey), new FlickrGuiModule());
            LOGGER.debug("Binding success");
            injector.getInstance(FlickrGuiController.class).run();
            LOGGER.debug("Controller succeed");
            injector.getInstance(Bootstrap.class).bootstrap();
            LOGGER.debug("Bootstrap succeed");
            
        } catch(Exception ex) {
            LOGGER.error("Error starting Flickr desktop", ex);
            System.exit(ExitService.FATAL_ERROR);
        }
    }
    
    private static void configureLogs() {
        try {
            LogManager.getLogManager().reset();
            JoranConfigurator configurator = new JoranConfigurator();
            LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
            lc.reset();
            configurator.setContext(lc);

            File file = new File("etc/logback.xml");
            if(file.exists()) {
                configurator.doConfigure(file);
            } else {
                configurator.doConfigure(FlickrDesktop.class.getResource("/com/flickr/desktop/core/logback.xml"));
            }
        } catch (JoranException ex) {
            ex.printStackTrace();
        }
    }
    
}
